//
//  FoundRmController.swift
//  KkmhSwift
//
//  Created by tenltrs on 2018/11/14.
//  Copyright © 2018 Tenltrs. All rights reserved.
//

import UIKit

class FoundRmController : BaseChildViewController,UITableViewDelegate,UITableViewDataSource{
    
    private var tableView : UITableView!
    
    private var rmModel : FoundRmModel?
    
    private var headerView : FoundRmHeaderView!
    
    private var isRefresh : Bool = true
    
    private var pageIndex : NSInteger = 0
    
    private var dataArray : [FoundRmInfoModel]! = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        initData()
        
        initMain()
    }
    
    func initData(){
        
        weak var ws = self
        RequestManager.GET(url: RequestUrls.topic_new_discovery_list, parameter: ["gender":App_Gender, "operator_count":"68"], target: self, isLoad: false, success: { (result) in
            ws?.tableView.mj_header.endRefreshing()
            if result.success{
                let model = FoundRmModel.mj_object(withKeyValues: result.data!)
                ws?.rmModel = model
                ws?.headerView.bannerModel=model?.infos?.first
                ws?.headerView.itemModel=model?.infos?[1]
                ws?.dataArray.removeAll()
                for (index,item) in (model?.infos?.enumerated())!{
                    if index>1{
                        ws?.dataArray.append(item)
                    }
                }
                ws?.tableView.reloadData()
                
            }
            logger.info(ws?.rmModel)
        }) { (error) in
            
        }
    }
    
    func initMain(){
        weak var ws = self
        tableView=UITableView.init(frame: view.bounds, style: UITableView.Style.plain).then({ (make) in
            make.delegate=self
            make.dataSource=self
            make.separatorStyle=UITableViewCell.SeparatorStyle.none
            
            make.register(cellType: FoundRmCell.self)
            
            if #available(iOS 11.0, *) {
                make.contentInsetAdjustmentBehavior = .never
            } else {
                automaticallyAdjustsScrollViewInsets = false
            }
            make.contentInset=UIEdgeInsets(top: 0, left: 0, bottom: TABBAR_HEIGHT, right: 0)
            
            //添加 加载刷新
            let refreshHeader = MJRefreshGifHeader.init(refreshingBlock: {
                ws?.isRefresh=true
                ws?.pageIndex=0
                ws?.initData()
            })
            
            let arr = [-15,-30,-45,-60,-75,-90,-105,-120,-135,-150,-165,-180,-195,-210,-225,-240,-255,-270,-285,-300,-315,-330,0]
            var imageArr = [UIImage]()
            for item in arr {
                imageArr.append(UIImage(named: "ic_pullrefresh_loading_17x17_")!.byRotate(DegreesToRadians(CGFloat(item)), fitSize: false)!)
            }
            
            refreshHeader?.setImages(imageArr, for:MJRefreshState.idle)
            refreshHeader?.lastUpdatedTimeLabel.isHidden=true
            refreshHeader?.stateLabel.isHidden=true
            make.mj_header=refreshHeader
            
        })
        headerView=FoundRmHeaderView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: 500))
        tableView.tableHeaderView = headerView
        view.addSubview(tableView)
        
    }
    
    // MARK: -- tableView代理
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return dataArray.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let model = dataArray[indexPath.row]
        let cell : FoundRmCell = tableView.dequeueReusableCell(for: indexPath)
        cell.fillData(model: model, indexPath: indexPath)
        return cell
    }
}
